package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.mapper.WmUserMapper;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.media.pojos.WmUser;
import com.usian.service.WmUserService;
import com.usian.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    @Autowired
    private WmUserMapper wmUserMapper;

    @Override
    public ResponseResult saveWmUser(WmUser wmUser) {
        if(wmUser ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmUser.setCreatedTime(new Date());
        wmUserMapper.insert(wmUser);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public WmUser findByName(String name) {

        if(StringUtils.isBlank(name)){
            return null;
        }

//        QueryWrapper<WmUser> queryWrapper =new QueryWrapper<>();
//        queryWrapper.lambda().eq(WmUser::getName, name);
//
//        LambdaQueryWrapper<WmUser> lambdaQueryWrapper =new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(WmUser::getName, name);

        WmUser wmUser = this.getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        return wmUser;
    }

    @Override
    public ResponseResult login(WmUserDto dto) {
        /**
         * 1:校验参数
         */
        if(dto==null ||(StringUtils.isBlank(dto.getName())||StringUtils.isBlank(dto.getPassword()))){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2:根据用户名去aduser表中查询有效用户 查出来aduser对象
        QueryWrapper<WmUser> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda().eq(WmUser::getName,dto.getName());
        WmUser wmUser = this.getOne(queryWrapper);
        if(wmUser!=null){
            //比较密码  当前用户输入密码串+数据库查出来的salt 拼接  生成新的md5密码串 ，比较新密码串和数据库中的密码串  一致 对  反之
            String psw =dto.getPassword()+ wmUser.getSalt();
            String newPwd = DigestUtils.md5DigestAsHex(psw.getBytes()); //md5密码串

            if(wmUser.getPassword().equals(newPwd)){
                //比较成功，生成token给前端(token,user)
                Map<String,Object> map =new HashMap<>();
                wmUser.setPassword("");
                wmUser.setSalt("");
                map.put("token", AppJwtUtil.getToken(wmUser.getId().longValue()));
                map.put("user",wmUser);
                return ResponseResult.okResult(map);
            }else{
                //失败
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }

        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
    }
}
